import java.util.Arrays;
import java.util.Scanner;

public class BinarySearch {
    /**
     * 二分搜索
     * @author ShakeJ
     * @date 2024/1/4
     */

    public static int search(int[] nums,int target){
        Arrays.sort(nums);

        int left=0;
        int right=nums.length-1;
        while(left<=right){
            int mid=left+(right-left)/2;
            if(nums[mid]==target){
                while(mid>0 && nums[mid-1]==target){
                    mid--;
                }
                return mid;
            }
            else if(nums[mid]<target){
                left=mid + 1;
            }else{
                right=mid-1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[]nums={1,3,2,5,4,3,3,4,5};
        Scanner in=new Scanner(System.in);
        System.out.print("输入目标值：");
        int target=in.nextInt();

        int a=search(nums,target);
        System.out.println(Arrays.toString(nums));
        System.out.println(a);
    }
}
